<?php

abstract class Kd_Web_Application {

//    protected $steps = array('initialize', 'route', 'filter', 'session', 'authenticate', 'authenticate', 'handler', 'output', 'finish');
    protected $currentStep = 'initialize';
    protected $nextStep = 'initialize';
    private $_config = array();
    
    public function __construct($config = array()) {
    	$this->_config = new Kd_Configuration($config);
    }
    
    public function getConfig(){
		return $this->_config;
    }

    protected function initialize() {
        $this->currentStep = 'initialize';
        $this->nextStep = 'route';
//    	set_exception_handler(array(Kd_Factory::findSingleton('mvc_exception_handler'), 'process'));
    }

    protected function route() {
        $this->currentStep = 'route';
        $this->nextStep = 'filter';
//        Kd_Web::getRouter()->parseUrl(Kd_Web::getRequest()->getUri());
    }

    protected function filter() {
        $this->currentStep = 'filter';
        $this->nextStep = 'session';
//        Kd_Web::getFilter()->filtrate();
    }

    protected function session(){
        $this->currentStep = 'session';
        $this->nextStep = 'authenticate';
        Kd_Web::getSession()->start();
//        if (Kd_Web::getRequest()->isGet()) {
//            Kd_Web::getHistory()->record($this->getContext()->getRouter()->getActionPath(), Kd_Web::getRequest()->getUri());
//        }
    }

    protected function authenticate() {
        $this->currentStep = 'authenticate';
        $this->nextStep = 'authorization';
    }

    protected function authorization(){
        $this->currentStep = 'authorization';
        $this->nextStep = 'handler';
    }

    protected function handler() {
        $this->currentStep = 'handler';
        $this->nextStep = 'output';
//        Kd_Web::getHandler()->process();
    }

    protected function output() {
        $this->currentStep = 'output';
        $this->nextStep = 'finish';
        Kd_Web::getResponse()->flush();
    }

    protected function finish() {
        $this->currentStep = 'finish';
        $this->nextStep = NULL;
        //Kd_Cache_Manager::saveAllCaches();
//        Kd_Web::getResponse()->end();
    }

    public function run() {
        while (!is_null($this->nextStep)){
            $method = $this->nextStep;
            $this->$method();
        }
    }
}